package com.shop.admin.controller.course;

import com.shop.admin.aop.Log;
import com.shop.admin.service.ICourseService;
import com.shop.admin.validate.commons.IdValidate;
import com.shop.admin.validate.course.CourseCreateValidate;
import com.shop.admin.validate.course.CourseUpdateValidate;
import com.shop.admin.validate.course.CourseSearchValidate;
import com.shop.admin.validate.commons.PageValidate;
import com.shop.admin.vo.course.CourseListedVo;
import com.shop.admin.vo.course.CourseDetailVo;
import com.shop.common.core.AjaxResult;
import com.shop.common.core.PageResult;
import com.shop.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("${app.api-prefix}/course")
@Api(tags = "课程管理")
public class CourseController {

    @Resource
    ICourseService iCourseService;

    @GetMapping("/list")
    @ApiOperation(value="课程列表")
    public AjaxResult<PageResult<CourseListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated CourseSearchValidate searchValidate) {
        PageResult<CourseListedVo> list = iCourseService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="课程详情")
    public AjaxResult<CourseDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        CourseDetailVo detail = iCourseService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "课程新增")
    @PostMapping("/add")
    @ApiOperation(value="课程新增")
    public AjaxResult<Object> add(@Validated @RequestBody CourseCreateValidate createValidate) {
        iCourseService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "课程编辑")
    @PostMapping("/edit")
    @ApiOperation(value="课程编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody CourseUpdateValidate updateValidate) {
        iCourseService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "课程删除")
    @PostMapping("/del")
    @ApiOperation(value="课程删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iCourseService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
